package com.feicheng.house.service.impl;

import com.feicheng.house.entity.BuyOrder;
import com.feicheng.house.exception.SystemException;
import com.feicheng.house.mapper.BuyOrderMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IBuyOrderService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderMapper, BuyOrder> implements IBuyOrderService {
	/** 订单类别头 */
    private static final String ORDER_CODE = "BR";
	

	@Override
	public ResultInfo addOne(BuyOrder buyOrder) {
		 DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");//生成时间戳
	        buyOrder.setBuyOrderNumber(ORDER_CODE + sdf.format(new Date()));

	        buyOrder.setOrderStatus(false);
			if (null==buyOrder.getBuyOrderNumber()) {
				throw new SystemException("请输入订单单号！");
			};
			if (null==buyOrder.getClientId()) {
				throw new SystemException("请输入客户编号！");
			};
			if (null==buyOrder.getShopId()) {
				throw new SystemException("请输入门店名！");
			};
			return ResultInfo.success(this.save(buyOrder));
			
		
	}

	@Override
	public ResultInfo updateOne(BuyOrder buyOrder) {

		return ResultInfo.success(this.updateById(buyOrder));
	}

	@Override
	public ResultInfo getCount(Integer clientId) {
		QueryWrapper<BuyOrder> buyOrderWrapper = new QueryWrapper<BuyOrder>();
		buyOrderWrapper.select("count(*)").eq("client_id", clientId);
		
		return ResultInfo.success(this.list(buyOrderWrapper));
	}

	
	

	

}
